<template>
    <div>
        <a href='/AddView'>新增</a><br>
        姓名<input type="text" v-model="qag.name">
        价钱<input type="text" v-model="qag.max">-<input type="text" v-model="qag.min">
        <input type="button" value="查询" @click="Show">
        <input type="button" value="升序" @click="ordery('asc')">
        <input type="button" value="降序" @click="ordery('dasc')">
        <table>
            <thead>
                <tr>
                    <td>姓名</td>
                    <td>手机号</td>
                    <td>房型</td>
                    <td>数量</td>
                    <td>价钱</td>
                    <td>图片</td>
                    <td>上下架</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="m in data">
                    <td>{{ m. name}}</td>
                    <td>{{ m. phone}}</td>
                    <td>{{ m. fent}}</td>
                    <td>{{ m. num}}</td>
                    <td>{{ m. price}}</td>
                    <td>
                        <img v-if="m.img" :src="'https://localhost:7087'+m.img" alt="" style="width: 80px;height: 80px;">
                    </td>
                    <td>{{ m.shen?"上架":"下架"}}</td>
                    <td>
                        <a :href="'https://localhost:7087'+m.img" target="_blank">看图片</a>
                        <input type="button" value="删除" @click="Del(m.id)">
                        <input type="button" value="编辑" @click="Xin(m.id)">
                        <input type="button" value="逻辑删除" @click="Delete(m.id)">
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="button" value="首页" @click="Fent(1)">
        <input type="button" value="上一页" @click="Fent(2)">
        <input type="button" value="下一页" @click="Fent(3)">
        <input type="button" value="尾页" @click="Fent(4)">
        一共{{ page.pcount }}页, 一共{{ page.count }}条,每页{{ qag.parsize }}条,当前的{{ qag.pardex }}页
        <select v-model="qag.parsize" @change="setsize">
            <option>2</option>
            <option>4</option>
            <option>6</option>
        </select>
        <input type="text" v-model="qag.pardex">
        <input type="button" value="跳转" @click="setdex">
    </div>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const rount=useRouter()

const ordery=(val:any)=>{
    qag.value.desc=val;
    Show()
}

const Xin=(vid:any)=>{
    rount.push({path:'/XinView',query:{id:vid}})
}

const Delete=(vid:any)=>{
    if(!confirm('确认逻辑删除吗?')){
        return;
    }
    axios.delete("https://localhost:7087/api/User/DeleteRang",{params:{id:vid}}).then(res=>{
        if(res.data>0){
            alert('逻辑删除成功')
            Show()
        }
    })
}

const Del=(vid:any)=>{
    if(!confirm('确认删除吗?')){
        return;
    }
    axios.delete("https://localhost:7087/api/User/Delete",{params:{id:vid}}).then(res=>{
        if(res.data>0){
            alert('删除成功')
            Show()
        }
        else{
            alert('删除失败')
        }
    })
}


onMounted(() => {
    Show()  
})

const data=ref([{
  "id": 0,
  "name": "",
  "pwd": "",
  "phone": "",
  "fent": "0",
  "num": 0,
  "price": 0,
  "img": "",
  "shen": true
}])

const setsize=()=>{
    qag.value.pardex=1;
    Show()
}

const setdex=()=>{
    if(qag.value.pardex>=1&&qag.value.pardex<=page.value.pcount){
        Show()
    }
}


const Fent=(val:any)=>{
    if(val==1){
        qag.value.pardex=1;
        Show()
    }
    if(val==2){
        if(qag.value.pardex>1){
            qag.value.pardex-=1;
            Show()
        }  
    }
    if(val==3){
        if(qag.value.pardex<page.value.pcount){
            qag.value.pardex+=1;
            Show()
        }
    }
    if(val==4){
        qag.value.pardex=page.value.pcount;
        Show()
    }
}

const qag=ref({
    pardex:1,
    parsize:2,
    name:"",
    max:0,
    min:0,
    desc:'desc'
})

const page=ref({
    count:0,
    pcount:0
})

const Show=()=>{
    
    axios.get("https://localhost:7087/api/User/Show",{params:qag.value}).then(res=>{
        data.value=res.data.data;
        page.value.count=res.data.count;
        page.value.pcount=Math.ceil(res.data.count/qag.value.parsize);
    })
}


</script>

<style scoped>

</style>